package tool

import (
	"crypto/hmac"
	"io"
	"math/rand"
	"regexp"

	MD5 "crypto/md5"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"strconv"
	"strings"
)

/**
 * @Description: 检测两组用空格分隔的字符串是否有相同的单词
 * @receiver this
 * @param findIn 原始字符串
 * @param find 要搜索的字符串
 * @return bool
 */
func HasSameWord(findIn string, find string) bool {
	var findInArr, findArr []string
	findInArr = strings.Fields(findIn)
	findArr = strings.Fields(find)
	for _, itemIn := range findInArr {
		for _, item := range findArr {
			if item == itemIn {
				return true
			}
		}
	}
	return false
}
func Ucfirst(str string) string {
	var upperStr string
	vv := []rune(str) // 后文有介绍
	for i := 0; i < len(vv); i++ {
		if i == 0 {
			if vv[i] >= 97 && vv[i] <= 122 { // 后文有介绍
				vv[i] -= 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {
				fmt.Println("Not begins with lowercase letter,", str)
				return str
			}
		} else {
			upperStr += string(vv[i])
		}
	}
	return upperStr
}
func Lcfirst(str string) string {
	var upperStr string
	vv := []rune(str) // 后文有介绍
	for i := 0; i < len(vv); i++ {
		if i == 0 {
			if vv[i] >= 65 && vv[i] <= 121 { // 后文有介绍
				vv[i] += 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {
				fmt.Println("Not begins with lowercase letter,")
				return str
			}
		} else {
			upperStr += string(vv[i])
		}
	}
	return upperStr
}

func Md5(str string) string {
	data := []byte(str)
	return fmt.Sprintf("%x", MD5.Sum(data))
}

func Int64ToInt(int64 int64) int {
	strInt64 := strconv.FormatInt(int64, 10)
	int_16, _ := strconv.Atoi(strInt64)
	return int_16
}

func Float64ToInt(int64 float64) int {
	strInt64 := strconv.FormatFloat(int64, 'e', 10, 64)
	int_16, _ := strconv.Atoi(strInt64)
	return int_16
}

func SHA256(message []byte) string {
	bytes2 := sha256.Sum256(message)           //计算哈希值，返回一个长度为32的数组
	hashcode2 := hex.EncodeToString(bytes2[:]) //将数组转换成切片，转换成16进制，返回字符串
	return hashcode2
}
func HmacHs256(message string, secret string) string {
	h := hmac.New(sha256.New, []byte(secret))
	io.WriteString(h, message)
	return fmt.Sprintf("%x", h.Sum(nil))
}
func CreateOrderSn(randLen int, prefix string, suffix string) string {
	base := prefix + GetDateYMDHIS("", "", "") + CreateRand(randLen) + suffix
	return base
}

func CreateRand(randLen int) string {
	//println(randLen)
	base := strconv.Itoa(Pow(10, randLen-1) + rand.Intn(8*Pow(10, randLen-1)+Pow(10, randLen-2)-1))
	return base
}
func CreatRandomInt(min, max int) int {
	return min + rand.Intn(max-min)
}
func CreateRandString(len int) string {
	bytes := make([]byte, len)
	for i := 0; i < len; i++ {
		bytes[i] = byte(CreatRandomInt(65, 90))
	}
	return string(bytes)
}
func StringArrayUnique(orgSlice []string) {
	var tmpMap = make(map[string]bool)
	for _, item := range orgSlice {
		tmpMap[item] = true
	}
	var backSlice []string
	for item, _ := range tmpMap {
		backSlice = append(backSlice, item)
	}
	orgSlice = backSlice
}

func MatchAll(str string, pattern string) [][]string {
	reg := regexp.MustCompile(pattern)
	if reg == nil {
		return nil
	}
	return reg.FindAllStringSubmatch(str, -1)
}
